package com.ruoyi.web.controller.business;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

import com.ruoyi.common.utils.ShiroUtils;
import org.apache.commons.io.IOUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.business.domain.CarPackageAudit;
import com.ruoyi.business.service.ICarPackageAuditService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

import javax.servlet.http.HttpServletResponse;

/**
 * 【请填写功能名称】Controller
 *
 * @author ruoyi
 * @date 2022-02-07
 */
@Controller
@RequestMapping("/business/carPackageAudit")
public class CarPackageAuditController extends BaseController {
    private String prefix = "business/carPackageAudit";

    @Autowired
    private ICarPackageAuditService carPackageAuditService;

    @RequiresPermissions("business:audit:view")
    @GetMapping("/listPage")
    public String audit() {
        return prefix + "/list";
    }

    /**
     * 查询【请填写功能名称】列表
     */
    @RequiresPermissions("business:audit:list")
    @PostMapping("/query")
    @ResponseBody
    public TableDataInfo list(CarPackageAudit carPackageAudit) {
        startPage();
        carPackageAudit.getParams().put("userId",ShiroUtils.getUserId());
        List<CarPackageAudit> list = carPackageAuditService.selectCarPackageAuditList(carPackageAudit);
        return getDataTable(list);
    }

    /**
     * 导出【请填写功能名称】列表
     */
    @RequiresPermissions("business:audit:export")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(CarPackageAudit carPackageAudit) {
        List<CarPackageAudit> list = carPackageAuditService.selectCarPackageAuditList(carPackageAudit);
        ExcelUtil<CarPackageAudit> util = new ExcelUtil<CarPackageAudit>(CarPackageAudit.class);
        return util.exportExcel(list, "【请填写功能名称】数据");
    }

    /**
     * 新增【请填写功能名称】
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存【请填写功能名称】
     */
    @RequiresPermissions("business:audit:add")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(CarPackageAudit carPackageAudit) {
        return toAjax(carPackageAuditService.insertCarPackageAudit(carPackageAudit));
    }

    /**
     * 修改【请填写功能名称】
     */
    @RequiresPermissions("business:audit:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap) {
        CarPackageAudit carPackageAudit = carPackageAuditService.selectCarPackageAuditById(id);
        mmap.put("carPackageAudit", carPackageAudit);
        return prefix + "/edit";
    }

    /**
     * 修改保存【请填写功能名称】
     */
    @RequiresPermissions("business:audit:edit")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(CarPackageAudit carPackageAudit) {
        return toAjax(carPackageAuditService.updateCarPackageAudit(carPackageAudit));
    }

    /**
     * 删除【请填写功能名称】
     */
    @RequiresPermissions("business:audit:remove")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(carPackageAuditService.deleteCarPackageAuditByIds(ids));
    }

    @RequestMapping("/todoPage")
    public String todoPage() {
        return prefix + "/todoPage";
    }
    @RequestMapping("/donePage")
    public String donePage() {
        return prefix + "/donePage";
    }
    @PostMapping("/startAudit")
    @ResponseBody
    public AjaxResult startAudit(Long id, Long bpmnInfoId, Long director, Long finance, String info) {
        carPackageAuditService.startAudit(id, bpmnInfoId, director, finance, info);
        return AjaxResult.success();
    }

    @RequestMapping("/cancelApply")
    @ResponseBody
    public AjaxResult cancelApply(Long id) {
        carPackageAuditService.cancelApply(id);
        return AjaxResult.success();
    }

    @RequestMapping("/processImg")
    @ResponseBody
    public void processImg(Long id, HttpServletResponse rep) throws IOException {
        InputStream inputStream = carPackageAuditService.processImg(id);
        IOUtils.copy(inputStream, rep.getOutputStream());
    }

    //审批
    @RequestMapping("/audit")
    @ResponseBody
    public AjaxResult audit(Long id,Long auditStatus,String info) {
        carPackageAuditService.audit(id,auditStatus,info);
        return AjaxResult.success();
    }
    @RequestMapping("/todoQuery")
    @ResponseBody
    public TableDataInfo todoQuery(CarPackageAudit carPackageAudit) {
        startPage();
        carPackageAudit.getParams().put("userId",ShiroUtils.getUserId());
        return getDataTable(carPackageAuditService.queryForRank(carPackageAudit));
    }

    //我的已办
     @RequestMapping("/doneQuery")
    @ResponseBody
    public TableDataInfo donePage(CarPackageAudit carPackageAudit) {
        startPage();
        carPackageAudit.getParams().put("userId",ShiroUtils.getUserId());
        return getDataTable(carPackageAuditService.queryForYet (carPackageAudit));
    }







}
